//
//  MapRequest.m
//  Cycling
//
//  Created by 迟国强 on 2023/3/26.
//

#import "MapRequest.h"
#import "BaseBodyModel.h"

#import "Cycling-Swift.h"

@implementation MapRequest
+ (void)getCyclingListWithPage:(int)page address:(NSString *)address day:(NSString *)day time:(NSString *)time result:(void (^)(NSDictionary * _Nonnull))resultBlock {
    NSString *url = [NSString stringWithFormat:@"%@%@",BaseUrl,Map_API_CyclingList];
    NSDictionary *parm = @{
                           @"page":@(page),
                           @"address":address,
                           @"day":day,
                           @"time":time,
                           @"pageSize":@"10"
                           };
    [self basePOST:url cache:NO autoHideLoading:NO parms:parm result:^(id  _Nullable resultObject, NSError * _Nullable error) {
        if(resultBlock){
            if (resultObject == nil) {
                resultBlock(nil);
            } else {
                BaseBodyModel *model = [BaseBodyModel yy_modelWithJSON:resultObject];
                NSDictionary *dic = model.data;
                if (model.code == 0) {
                    [HUDHelper showHudTipStr:model.msg];
                    resultBlock(nil);
                } else {
                    resultBlock(dic);
                }
            }
            
        }
    }];
}

+ (void)searchRecordWithResult:(void(^)(NSDictionary *dic))resultBlock {
    NSString *url = [NSString stringWithFormat:@"%@%@",BaseUrl,Map_API_SearchRecord];
    
    [self basePOST:url cache:NO autoHideLoading:NO parms:nil result:^(id  _Nullable resultObject, NSError * _Nullable error) {
        if(resultBlock){
            if (resultObject == nil) {
                resultBlock(nil);
            } else {
                BaseBodyModel *model = [BaseBodyModel yy_modelWithJSON:resultObject];
                NSDictionary *dic = model.data;
                if (model.code == 0) {
                    [HUDHelper showHudTipStr:model.msg];
                    resultBlock(nil);
                } else {
                    resultBlock(dic);
                }
            }
            
        }
    }];
}

@end

/**
 导航请求
 */
@implementation MapRequest (CSNavigationInMap)

+ (void)postStartNavigationWithParams:(NSDictionary *)params complete:(void(^)(BOOL success, NSInteger cid))complete
{
    NSMutableDictionary *realParams = [params mutableCopy];
    NSString *url = [NSString stringWithFormat:@"%@%@",BaseUrl,Map_Riding_API_Start];

    [self basePOST:url cache:NO autoHideLoading:NO parms:realParams result:^(id resultObject, NSError *error) {
        if(resultObject == nil) {
            [HUDHelper showHudTipStr:@"未知错误，请稍后重试"];
            if(complete != nil) {
                complete(NO, 0);
            }
        } else {
            BaseBodyModel *baseModel = [BaseBodyModel yy_modelWithJSON:resultObject];
            if(baseModel.code == 0) {
                [HUDHelper showHudTipStr:baseModel.msg];
                if(complete != nil) {
                    complete(NO, 0);
                }
            } else {
                if(complete) {
                    complete(YES, [[baseModel.dicOfData valueForKey:@"cid"] integerValue]);
                }
            }
        }
    }];
}

+ (void)postNavigatingDataWithParams:(NSDictionary *)params complete:(void(^)(BOOL success, NSString *reason))complete
{
    NSString *url = [NSString stringWithFormat:@"%@%@",BaseUrl,Map_Riding_API_HeartBeat];

    [self basePOST:url cache:NO autoHideLoading:NO parms:params result:^(id resultObject, NSError *error) {
        if(resultObject == nil) {
            if(complete != nil) {
                complete(NO, @"数据未返回");
            }
        } else {
            BaseBodyModel *baseModel = [BaseBodyModel yy_modelWithJSON:resultObject];
            if(baseModel.code == 0) {
                [HUDHelper showHudTipStr:baseModel.msg];
                if(complete != nil) {
                    complete(NO, baseModel.msg);
                }
            } else {
                if(complete) {
                    complete(YES, @"请求成功");
                }
            }
        }
    }];
}

/**
 Post方式上传数据(仅返回一个成功或者失败的状态)
 */
+ (void)postMapRequestAndRequestStatusFoAPI:(NSString*)api andParams:(NSDictionary *)params complete:(void(^)(BOOL success, NSString *reason))complete
{
    [self postMapRequestAndRequestStatusFoAPI:api andParams:params autoShowSuccessNotice:false complete:complete];
}

+ (void)postMapRequestAndRequestStatusFoAPI:(NSString*)api andParams:(NSDictionary *)params autoShowSuccessNotice:(BOOL)willShow complete:(void(^)(BOOL success, NSString *reason))complete
{
    NSString *url = [NSString stringWithFormat:@"%@%@",BaseUrl,Map_Riding_API_Suspendend];

    [self basePOST:url cache:NO autoHideLoading:NO parms:params result:^(id resultObject, NSError *error) {
        if(resultObject == nil) {
            if(complete != nil) {
                complete(NO, @"数据未返回");
            }
        } else {
            BaseBodyModel *baseModel = [BaseBodyModel yy_modelWithJSON:resultObject];
            if(baseModel.code == 0) {
                if(complete != nil) {
                    complete(NO, baseModel.msg);
                }
            }  else {
                if(complete) {
                    complete(YES, @"请求成功");
                }
            }
            if(willShow) {
                [HUDHelper showHudTipStr:baseModel.msg];
            }
        }
    }];
}

@end
